import { createRouter, createWebHistory } from 'vue-router'
import { useUserInfoStore } from '@/stores/userInfo'
import { getToken, removeToken } from '@/utils/token-utils'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: 'home',
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/pages/login/index.vue')
    },
    //首页
    {
      path: '/home',
      name: 'Home',
      component: () => import('@/pages/home/index.vue'),
    },
    //行情
    {
      path: '/market',
      name: 'Market',
      component: () => import('@/pages/market/index.vue')
    },
    //交易
    {
      path: '/trade',
      name: 'Trade',
      component: () => import('@/pages/trade/index.vue')
    },
    //圈子
    {
      path: '/circle',
      name: 'Circle',
      component: () => import('@/pages/circle/index.vue')
    },
    //我的
    {
      path: '/my',
      name: 'My',
      component: () => import('@/pages/my/index.vue')
    },
    //产品库
    {
      path:'/productLibrary',
      name:'ProductLibrary',
      component: () => import('@/pages/home/productLibrary/index.vue')
    },
    //茶品牌
    {
      path:'/teaBrand',
      name:'TeaBrand',
      component: () => import('@/pages/home/teaBrand/index.vue')
    }
  ],
})
//全局的前置守卫
// router.beforeEach( async (to, form, next) => {
//   const userInfoStore = useUserInfoStore()
//   const token = getToken()
  
//   const userInfo = !!userInfoStore.nickName
//   if(token){
//     //已经登录过，在进入登录页面，直接跳转到主页
//     if(to.path == '/login'){
//       next({path: "/"})
//     }else{
//       if(userInfo){
//         //任何页面都能进
//         next()
//       }else{
//         try{
//           //没有nickName,第一次登录
//           await userInfoStore.getInfo()
//           next()
//         }catch(error){
//           //自带的token可以有问题，我删除
//           removeToken()
//         }
//       }
//     }
//   }else{
//     //没有token,这是没有登录，直接放行
//     next()
//   }
// })


//全局的后置守卫

export default router
